// pages/quadratic/quadratic.js
var utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    equation: '3x²+21x+5',
    isTopRetMinusX: false,
    isTopRetMinusY: false,
    showRet: false,
    squareNum: 0,
    topPoint: {
      x: {
        fenzi: 0,
        fenmu: 0
      },
      y: {
        fenzi: 0,
        fenmu: 0
      }
    },
    crossPointY: {
      fenzi: 0,
      fenmu: 0
    }
  },

  // 函数输入框值改变事件
  inputEquation: function(e) {
    // (-) ?\d * x²[-\+]\d * x(-|\+)(\d +)$
    this.setData({
      equation: e.detail.value
    })
  },

  // 为输入框添加x²
  addSquare: function() {
    let equation = this.data.equation + 'x²'
    this.setData({
      equation: equation
    })
  },

  //计算按钮点击函数
  count: function() {
    let equation = this.data.equation
    let isMatch = equation.match(/(-)?\d*(\.\d+)?x²([-\+]\d*(\.\d+)?x)?((-|\+)\d+(\.\d+)?)?/gi)
    if (isMatch) {
      console.log('符合')
      let a, b, c
      // 取得a的值
      let aArr = equation.match(/.+(?=x²)/gi)
      if (aArr) {
        aArr[0] === '-' ? a = -1 : a = parseFloat(aArr[0])
      } else {
        a = 1
      }
      console.log('a的值是', a)
      // 取得b值
      let bArr = equation.match(/(?<=x²).+(?=x)/ig)
      if (bArr) {
        bArr[0] === '-' ? b = -1 : b = parseFloat(bArr[0])
      } else {
        // 解析式包含x并且x前无数字则b=1
        if (equation.indexOf('x') > 0) {
          b = 1
        } else {
          // 解析式没有x则b=0
          b = 0
        }
      }
      console.log('b的值是', b)
      // 取得c值
      let cArr = equation.match(/(\+|-)\d*(\.\d+)?$/ig)
      if (cArr) {
        c = parseFloat(cArr[0])
      } else {
        c = 0
      }
      console.log('c的值是', c)
      let fenmu = -2 * a
      console.log('分母是', fenmu)
      let topPointX = utils.getShang(b, fenmu)
      console.log('顶点横坐标是', topPointX)

      let fenzi = 4 * a * c - b * b;
      fenmu = 4 * a
      console.log('y交点的分子是', fenzi)
      console.log('y的分母是', fenmu)
      let topPointY = utils.getShang(fenzi, fenmu)
      console.log('y的顶点坐标是', topPointY)

      let judgeEquation
      let num = b * b - 4 * a * c
      if (num < 0) {
        judgeEquation = '无实根'
      } else {
        judgeEquation = utils.getSquare(num)
      }
      console.log('num是', num)

      console.log('判别式是', judgeEquation)

      this.setData({
        topPoint: {
          x: topPointX,
          y: topPointY
        },
        crossPointY: {
          fenzi: c
        },
        squareNum: judgeEquation,
        showRet: true,
        isTopRetMinusX: topPointX.isRetMinus,
        isTopRetMinusY: topPointY.isRetMinus
      })
    } else {
      console.log('不符合')
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    utils.getSquare(54)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})